package com.tracbds.api.common;

import com.tracbds.core.cmd.Cmd8600;
import com.tracbds.core.service.JT808CommandService;
import com.tracbds.core.bean.Cmd8600Bean;
import com.lingx.web.api.IApi;
import com.lingx.web.api.impl.AbstractAuthApi;
import com.lingx.utils.TokenUtils;
import com.lingx.utils.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 下发JT808 0x8600圆形区域命令
 */
@Component
public class Api18600 extends AbstractAuthApi implements IApi {
    @Autowired
    private JT808CommandService commandService;
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public int getApiCode() {
        return 18600;
    }

    @Override
    public String getApiName() {
        return "下发圆形区域(0x8600)";
    }

    @Override
    public String getGroupName() {
        return "协议扩展";
    }

    @Override
    public Map<String, Object> api(Map<String, Object> params) {
        Map<String, Object> ret = IApi.getRetMap(1, "SUCCESS");
        try {
            String token = IApi.getParamString(params, "lingxtoken", "");
            String car_id = IApi.getParamString(params, "car_id", "");
            String tid = IApi.getParamString(params, "tid", "");
            String userId = TokenUtils.getTokenDataUserId(token);
            double lng = Double.parseDouble(IApi.getParamString(params, "lng", "119.10305563788944"));
            double lat = Double.parseDouble(IApi.getParamString(params, "lat", "33.55349623399374"));
            int radius = Integer.parseInt(IApi.getParamString(params, "radius", "30"));
            int areaId = Integer.parseInt(IApi.getParamString(params, "area_id", "34"));
            int property = Integer.parseInt(IApi.getParamString(params, "property", "40")); // 0x28=40
            boolean isVersion = false;
            // 查询设备协议版本
            List<Map<String, Object>> list = this.jdbcTemplate.queryForList("select version from tgps_car where id=?", car_id);
            if (list.size() > 0) {
                Object versionObj = list.get(0).get("version");
                String v = versionObj == null ? "" : versionObj.toString();
                if (Utils.isNotNull(v)) {
                    isVersion = v.contains("2019");
                }
            }
            // 构造区域
            Cmd8600Bean area = new Cmd8600Bean(areaId, property, lat, lng, radius);
            List<Cmd8600Bean> areas = new ArrayList<>();
            areas.add(area);
            // 构造命令
            Cmd8600 cmd = new Cmd8600(tid, areas, isVersion);
            // 下发命令
            ret = commandService.sendCommand(cmd, car_id, userId, true);
            ret.put("tid", tid);
            ret.put("area_id", areaId);
            return ret;
        } catch (Exception e) {
            e.printStackTrace();
            ret.put("code", -1);
            ret.put("message", e.getLocalizedMessage());
            return ret;
        }
    }
} 